﻿using System.Collections.Generic;
using SG.Lang.Dry.Controlling;
using SG.Lang.Dry.Mirrors;

namespace SG.Lang.Dry.Code
{
    public class Block : Expression
    {
        private readonly IEnumerable<Expression> _elements;
        private readonly Expression _returnValue;

        private Block(ref ElementRecord elementRecord, ref ExpressionRecord expressionRecord)
            : base(ref elementRecord, ref expressionRecord)
        {
            ItemList elements;

            _elements = expressionRecord.Left.ResolveItemListNode(out elements)
                            ? elements.ResolveExpressions()
                            : new Expression[0];

            _returnValue = expressionRecord.Right.ResolveExpression();
        }

        public IEnumerable<Expression> Elements { get { return _elements; } } 
        public Expression ReturnValue { get { return _returnValue; } }

        public static Block Create(ref ElementRecord elementRecord, ref ExpressionRecord expressionRecord)
        {
            return new Block(ref elementRecord, ref expressionRecord);
        }
    }
}
